<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class b2c_ctl_admin_goods_opinions extends desktop_controller{
    /*
        %1 - id
        %2 - title
        $s - string
        $d - number
    */
    var $workground = 'b2c_ctl_admin_goods_opinions';

    public function __construct($app){
        parent::__construct($app);
        header("cache-control: no-store, no-cache, must-revalidate");
    }

    function index(){

        $actions_base['title'] = app::get('b2c')->_('推荐列表');
        $actions_base['use_view_tab'] = true;
        $actions_base['use_buildin_recycle'] = false;

        $this->finder('b2c_mdl_goods_opinions',$actions_base);
    }
    function comment($opinions_id){
        $actions_base['title'] = app::get('b2c')->_('推荐评论列表');
        $actions_base['use_view_tab'] = true;
        $actions_base['use_buildin_recycle'] = false;
        $actions_base['actions'] = array(array(
            'label'=>app::get('b2c')->_('返回'),
	    'href'=>'index.php?app=b2c&ctl=admin_goods_opinions&act=index',
            ),
            );
        $actions_base['base_filter'] = array(
            'opinions_id' => $opinions_id,
        );
        $this->finder('b2c_mdl_goods_opinions_comment',$actions_base);
    }
    function praise($opinions_id){
        $actions_base['title'] = app::get('b2c')->_('推荐点赞列表');
        $actions_base['use_view_tab'] = true;
        $actions_base['use_buildin_recycle'] = false;
        $actions_base['actions'] = array(array(
            'label'=>app::get('b2c')->_('返回'),
	    'href'=>'index.php?app=b2c&ctl=admin_goods_opinions&act=index',
            ),
            );
        $actions_base['base_filter'] = array(
            'opinions_id' => $opinions_id,
        );
        $this->finder('b2c_mdl_goods_opinions_praise',$actions_base);
    }
    function fancy($opinions_id){
        $this->begin('index.php?app=b2c&ctl=admin_goods_opinions&act=index');
        $opinions = app::get('b2c')->model("goods_opinions");
        $opinions_item = $opinions->getRow("*",array('id'=>$opinions_id));
        if($opinions_item){
            if($opinions_item['fancy']){
                $this->do_exp("opinions_fancy", $opinions_item['member_id'],false);
                $opinions->update(array('fancy'=>0),array('id'=>$opinions_id));
            }else{
                $this->do_exp("opinions_fancy", $opinions_item['member_id']);
                $opinions->update(array('fancy'=>1),array('id'=>$opinions_id));
            }
        }else{
            $this->end(false,app::get('b2c')->_('不存在推荐'));
        }
        $this->end(true,app::get('b2c')->_('设置成功'));
    }
    
    
    function do_exp($key,$member_id,$type=true){
            $exps = array(
                'upload_avatar' => 5,
                'opinions_praise' => 1,
                'get_praise' => 2,
                'opinions_comment' => 2,
                'opinions' => 20,
                'opinions_fancy' => 80,
                
            );
            $exp = $type ? $exps[$key] : 0-(int)$exps[$key];
            $member = app::get("b2c")->model("members")->getRow("*",array('member_id'=>$member_id));
            app::get("b2c")->model("members")->update(array('experience'=>$member['experience']+$exp),array('member_id'=>$member_id));
            
        }
    function image($opinions_id){
        $actions_base['title'] = app::get('b2c')->_('推荐标签列表');
        $actions_base['use_view_tab'] = true;
        $actions_base['use_buildin_recycle'] = false;
        $actions_base['actions'] = array(array(
            'label'=>app::get('b2c')->_('返回'),
	    'href'=>'index.php?app=b2c&ctl=admin_goods_opinions&act=index',
            ),
            );
        $actions_base['base_filter'] = array(
            'opinions_id' => $opinions_id,
        );
        $this->finder('b2c_mdl_goods_opinions_image',$actions_base);
    }
//    function addnew($nCatId = 0){
//        $this->_info($nCatId);
//    }



//     function save(){
//         $this->begin('index.php?app=b2c&ctl=admin_goods_cat&act=index');
//
//        if($this->app->model('goods_opinions')->save($_POST['cat'])){
//            
//            $this->end(true,app::get('b2c')->_('保存成功'));
//        }else{
//            $this->end(false,app::get('b2c')->_('保存失败'));
//        }
//    }

    function toRemove($id){
        $this->begin('index.php?app=b2c&ctl=admin_goods_opinions&act=index');
        $db = kernel::database();
        $filter['id'] = $id;
        $opinions = app::get('b2c')->model('goods_opinions')->getRow("*",array('id'=>$id));
        $transaction_status = $db->beginTransaction();
        if(!$this->app->model('goods_opinions')->delete($filter)){
            $db->rollback();
            $this->end(false, "删除主表失败");
        }
        app::get('b2c')->model('goods_opinions_comment')->delete(array(
            'opinions_id' => $id,
        ));
        app::get('b2c')->model('goods_opinions_praise')->delete(array(
            'opinions_id' => $id,
        ));
        app::get('b2c')->model('order_items')->update(array('is_opinions'=>0),array('order_id'=>$opinions['order_id'],'goods_id'=>$opinions['goods_id']));
        $db->commit($transaction_status);
        $this->end(true,app::get('b2c')->_('已删除'));

    }
    function toRemoveComment($id){
        $this->begin('index.php?app=b2c&ctl=admin_goods_opinions&act=comment');
        $objCat = $this->app->model('goods_opinions_comment');

        if($objCat->delete(array('id'=>$id))){
            $opinions = $objCat->get_row("*",array('id'=>$id));
            $counts = $objCat->count(array('opinions_id'=>$opinions['opinions_id']));
            app::get('b2c')->model('goods_opinions')->update(array('p_num'=>$counts),array('id'=>$opinions['opinions_id']));
            $this->end(true,app::get('b2c')->_('已删除'));

        }
        $this->end(false, $msg);
    }
    function toRemovePraise($id){
        $this->begin('index.php?app=b2c&ctl=admin_goods_opinions&act=praise');
        $objCat = $this->app->model('goods_opinions_praise');

        if($objCat->delete(array('id'=>$id))){
            $opinions = $objCat->get_row("*",array('id'=>$id));
            $counts = $objCat->count(array('opinions_id'=>$opinions['opinions_id']));
            app::get('b2c')->model('goods_opinions')->update(array('p_num'=>$counts),array('id'=>$opinions['opinions_id']));
            $this->end(true,app::get('b2c')->_('已删除'));

        }
        $this->end(false, $msg);
    }
    function toRemoveImage($id){
        $this->begin('index.php?app=b2c&ctl=admin_goods_opinions&act=image');
        $objCat = $this->app->model('goods_opinions_image');

        if($objCat->delete(array('id'=>$id))){
            $this->end(true,app::get('b2c')->_('已删除'));

        }
        $this->end(false, $msg);
    }


}
